package com.tan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.domain.Blog;
import com.tan.domain.Comment;
import com.tan.domain.vo.BlogVO;
import com.tan.domain.vo.CommentVO;
import com.tan.mapper.BlogMapper;
import com.tan.mapper.CommentMapper;
import com.tan.service.BlogService;
import com.tan.service.CommentService;
import com.tan.service.UserService;
import com.tan.util.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author longshifu
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
        implements BlogService {

    @Autowired
    BlogMapper blogMapper;
    @Autowired
    BlogService blogService;

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;

    @Override

    public List<BlogVO> listRecommendBlogTop(Integer size) {

        List<BlogVO> blogList = blogMapper.listAllBlog();
        //System.out.println(blogList);
        blogList.forEach(b -> {
            b.setContent("");
            b.setComments(null);
        });
        blogList = blogList.stream()
                .limit(size)
                .sorted(Comparator.comparing(BlogVO::getCreateTime).reversed()).collect(Collectors.toList());
        return blogList;
    }

    /**
     * 通过标题或分类分页查询
     * @param title
     * @param typeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public JSONObject getPageByTitleOrTypeId(String title, Integer typeId, int pageNum, int pageSize) {

        List<BlogVO> blogList = blogMapper.getAllByTitleOrTypeId(title, typeId);
        List<BlogVO> newBlogList = blogList.stream()
                .skip((long) (pageNum - 1) * pageSize).limit(pageSize)
                .sorted(Comparator.comparing(BlogVO::getCreateTime).reversed())
                .collect(Collectors.toList());
        JSONObject result = new JSONObject();
        result.put("blogs", newBlogList);
        result.put("total", blogList.size());
        return result;

    }

    @Override
    public BlogVO getBlogDetail(Long id) {
        BlogVO blog = blogMapper.getBlogById(id);
        //阅读量加1
        blog.setViews(blog.getViews()+1);
        Blog saveBlog = new Blog();
        BeanUtils.copyProperties(blog, saveBlog);
        blogMapper.updateByPrimaryKeySelective(saveBlog);
        String content = blog.getContent();
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Comment::getBlogId, blog.getId());
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentVO> commentVOS = new LinkedList<>();
        comments.forEach(e->{
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(e,vo);
            vo.setParentComment(commentMapper.selectById(vo.getParentCommentId()));
            commentVOS.add(vo);
        });
        blog.setComments(commentVOS);
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return blog;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public JSONObject getAllBlogPage(int pageNum, int pageSize) {
        List<BlogVO> blogList = blogMapper.listAllBlog();
        List<BlogVO> newBlogList = blogList.stream()
                .skip((long) (pageNum - 1) * pageSize).limit(pageSize)
                .sorted(Comparator.comparing(BlogVO::getCreateTime).reversed())
                .collect(Collectors.toList());
        System.out.println("数量： " +newBlogList.size());
        JSONObject result = new JSONObject();
        result.put("blogs", newBlogList);
        result.put("total", blogList.size());
        return result;
    }
}




